<!-- 开发部：开发材料建档 蹇泽阳 -->
<template>
  <div class="app-container">
    <el-card class="MainCard" shadow="always" :body-style="{ padding: '0' }">
      <!-- card body -->
      <div class="flex mt10">
        <div class="MainCardDivMain">
          <el-tree accordion class="tree" :data="data" :props="defaultProps" @node-click="handleNodeClick" />
        </div>
        <div class="ml10 mr10">
          <el-input v-model="input1" disabled size="small" clearable class="input mb18"></el-input>
          <Table :tableData="tableData6" :tableHeader="tableHeader" @currentRow="currentRow" />
        </div>
        <div class="mr10">
          <div class="flex">
            <span class="mr10" style="width: 60px">细分类</span>
            <el-input v-model="smallCategory" size="small" clearable class="mb18"></el-input>
          </div>
          <Find :OriginalFilter="{ static: true }" :formList="formList" :searchImg="false" @findClicked="findClicked" />
          <el-card shadow="never" :body-style="{ padding: '0px' }">
            <div slot="header" class="mb9 f16 mt10">
              <span style="font-weight: 600">查询条件</span>
              <span class="ml20 f12" style="color: red">双击清除当前查询</span>
            </div>
            <Find
              :OriginalFilter="{ static: true }"
              :formList="formList2"
              :searchImg="false"
              @findClicked="findClicked2"
              @inputDBclick="inputDBclick"
            />
          </el-card>
          <el-card shadow="never" :body-style="{ padding: '0px' }">
            <div slot="header" class="mb9 f16 mt10">
              <span style="font-weight: 600">材料查询</span>
              <el-button class="primaryBth ml20" type="primary" size="default" @click="removeOne">-</el-button>
            </div>
            <Table
              :tableData="tableData"
              :tableHeader="tableHeader2"
              :isLoading="isLoading"
              @selectListShow="selectListShow"
              :outTableHeight="170"
              :getTableList="getTableList"
              @confirmChange="confirmChange"
              @currentRow="currentRow2"
            />
          </el-card>
        </div>

        <!-- 宽幅 -->
        <div class="mr10">
          <el-input v-model="input3" size="small" disabled clearable class="input mb18"></el-input>
          <Table :tableData="tableData3" :tableHeader="tableHeader3" @currentRow="currentRow3" :outTableHeight="0" />
        </div>
        <!-- 单位 -->
        <div class="mr10">
          <el-input v-model="input4" size="small" disabled clearable class="input mb18"></el-input>
          <Table :tableData="tableData4" :tableHeader="tableHeader4" @currentRow="currentRow4" :outTableHeight="0" />
        </div>
      </div>
      <!-- 底部按钮 -->
      <el-card shadow="never" :body-style="{ padding: '5px' }">
        <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
      </el-card>
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import { formateData } from '@/plugins/mixinJ'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'DevelopMaterials'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch, onBeforeMount } from 'vue'
import { ref } from 'vue'
interface Tree {
  children?: Tree[]
  levelCode: string
  oldLevel: string
  parentLevel: string
  typeName: string
  typeNo: string
  mtlProperty: string
  remark: string
  sizeFlag: string
}
const input1 = ref('')
const input2 = ref('')
const input3 = ref('')
const input4 = ref('')
const smallCategory = ref('')
let typeName = ref('')
let typeName2 = ref('')
let typeName3 = ref('')
let typeNo = ref('')
let typeNo2 = ref('')
let typeNo3 = ref('')
const formList = ref([
  {
    type: 'button',
    checkboxLabel: '组成材料'
  },
  {
    type: 'button',
    checkboxLabel: '插入材料'
  },
  {
    type: 'input',
    inputLabel: '分类',
    inputPlaceholder: '',
    inputValue: typeName3,
    inputDisabled: true
  },
  {
    type: 'input',
    inputLabel: '材料名称',
    inputPlaceholder: '',
    inputValue: '',
    inputDisabled: true,
    inputWidth: '200px'
  },
  {
    type: 'input',
    inputLabel: '规格',
    inputPlaceholder: '',
    inputValue: '',
    inputDisabled: true
  },
  {
    type: 'input',
    inputLabel: '单位',
    inputPlaceholder: '',
    inputDisabled: true
  },
  {
    type: 'input',
    inputLabel: '编码',
    inputPlaceholder: '',
    inputValue: '',
    inputDisabled: true
  }
])
const findClicked = async (content: string) => {
  if (content === '组成材料') {
    if (smallCategory.value === '') return JMessage.error('细分类不能为空')
    const res = await LoginWork.post<any, any>(`/material/mtl/createSdMtlNew?choose=${1}`, {
      typeName: typeName3.value,
      mtlName: input1.value + ' ' + smallCategory.value,
      mtlSpec: input3.value,
      mtlUnit: input4.value
    })
    if (res.code === 200) {
      JMessage.success('组成材料成功')
      formList.value[3].inputValue = res.data.mtlName
      formList.value[4].inputValue = res.data.mtlSpec
      formList.value[5].inputValue = res.data.mtlUnit
      formList.value[6].inputValue = res.data.mtlNo
      // getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '插入材料') {
    const res = await LoginWork.post<any, any>(`/material/mtl/createSdMtlNew?choose=${2}`, {
      typeName: typeName3.value,
      mtlName: input1.value + ' ' + smallCategory.value,
      mtlSpec: input3.value,
      mtlUnit: input4.value,
      mtlNo: formList.value[6].inputValue
    })
    if (res.code === 200) {
      JMessage.success('插入材料成功')
      formList.value[3].inputValue = ''
      formList.value[4].inputValue = ''
      formList.value[5].inputValue = ''
      formList.value[6].inputValue = ''
      getTableList()
    } else {
      JMessage.error(res.message)
    }
  }
}
const mtlProperty = ref('')
const handleNodeClick = async (data: Tree) => {
  if (data.levelCode.length === 2) {
    typeName.value = data.typeName
    typeNo.value = data.typeNo
  } else if (data.levelCode.length === 4) {
    typeName2.value = data.typeName
    typeNo2.value = data.typeNo
  } else if (data.levelCode.length === 6) {
    typeName3.value = data.typeName
    typeNo3.value = data.typeNo
  }
  if (data.children === null) {
    const res = await LoginWork.post<any, any>('/material/mtl/property/sdMtlTypeDetailList', {
      typeName: typeName.value,
      typeName2: typeName2.value,
      typeName3: typeName3.value,
      typeNo: typeNo.value,
      typeNo2: typeNo2.value,
      typeNo3: typeNo3.value
    })
    mtlProperty.value = res.data[0] ? res.data[0].mtlProperty : ' '
    const res2 = (await service.get(`/material/mtl/property/mtlPropertyRD?typeName=${mtlProperty.value}`)) as any
    tableData6.value = res2

    console.log(data)
  }
}

const defaultProps = {
  children: 'children',
  label: 'typeName'
}
const tableData4 = ref([]) as any
const tableData3 = ref([]) as any
const tableData6 = ref([])
const tableData = ref([])
const tableHeader = ref([
  {
    label: mtlProperty || '',
    prop: 'mtlSpec'
  }
])
const tableHeader2 = ref([
  {
    change: true,
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    change: true,
    label: '材料名称',
    prop: 'mtlName',
    width: 200
  },
  {
    change: true,
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    change: true,
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    change: true,
    label: '建档人员',
    prop: 'sysUser'
  },
  {
    change: true,
    label: '建档日期',
    prop: 'sysDate',
    width: 200
  },
  {
    type: 'select',
    label: '厂商',
    prop: 'vendorName'
  },
  {
    label: '客户',
    prop: 'custom'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
const tableHeader3 = ref([
  {
    label: '宽幅',
    prop: 'mtlSpec'
  }
])
const tableHeader4 = ref([
  {
    label: '单位',
    prop: 'mtlUnit'
  }
])

const formList2 = ref([
  {
    type: 'input',
    inputLabel: '分类',
    inputPlaceholder: '',
    inputValue: typeName3,
    inputDisabled: true
  },
  {
    type: 'input',
    inputLabel: '规格',
    inputPlaceholder: '',
    inputValue: input1,
    inputDisabled: true
  },
  {
    type: 'input',
    inputPlaceholder: '请输入料号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入品名',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入品名2',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入品名3',
    inputValue: ''
  },
  {
    type: 'input',
    inputLabel: '宽幅',
    inputPlaceholder: '',
    inputValue: input3,
    inputDisabled: true
  },
  {
    type: 'input',
    inputLabel: '单位',
    inputPlaceholder: '',
    inputValue: input4,
    inputDisabled: true
  },
  {
    type: 'button',
    checkboxLabel: '置空'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])

const data = ref<any>([])
//获取所有部门资料
onMounted(() => {
  DepartmentalData(0)
})
const DepartmentalData = async (choose: number) => {
  let resolve = []
  if (choose === 0) {
    resolve = await service.get<any, any>('/material/mtl/property/mtlPropertyListTree')
  } else {
    resolve = await service.get<any, any>(`/material/mtl/property/sdCustTypeAVen?choose=${choose}`)
  }
  data.value = resolve
}

const currentRow = (row: any) => {
  input1.value = row.mtlSpec
  // console.log(row);
  // {mtlSpec: '0.55MM', mtlSeq: '80'}
}
const currentTable1Row = ref({}) as any
const currentRow2 = (row: any = tableData.value[0]) => {
  currentTable1Row.value = row
}
const currentRow3 = (row: any) => {
  input3.value = row.mtlSpec
  // console.log(row);
}
const currentRow4 = (row: any) => {
  input4.value = row.mtlUnit
  // console.log(row);
}

const getSelectList = async () => {
  const res = (await service.get('/material/mtl/property/mtlPropertyList')) as any
  // 宽幅
  tableData3.value = res.spec
  // 单位定义
  tableData4.value = res.unit
}
onBeforeMount(() => {
  getSelectList()
})

// 表一模块
// 表一按钮
const findClicked2 = async (content: string) => {
  if (content === '置空') {
    formList2.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/material/mtl/downloadSdMtlNew`, {
      mtlName1: formList2.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入品名'
      )?.inputValue,
      mtlName2: formList2.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入品名2'
      )?.inputValue,
      mtlName3: formList2.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入品名3'
      )?.inputValue,
      mtlNo: formList2.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入料号'
      )?.inputValue,
      mtlSpec1: input1.value,
      mtlSpec2: input3.value,
      mtlUnit: input4.value,
      typeName: typeName3.value,
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/material/mtl/sdMtlNewList', {
    mtlName1: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入品名'
    )?.inputValue,
    mtlName2: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入品名2'
    )?.inputValue,
    mtlName3: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入品名3'
    )?.inputValue,
    mtlNo: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入料号'
    )?.inputValue,
    mtlSpec1: input1.value,
    mtlSpec2: input3.value,
    mtlUnit: input4.value,
    typeName: typeName3.value,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list.map((item: any, index: number) => {
    item.vendorName = {
      selectValue: item.vendorName || '',
      options: []
    }
    item.change = 'Y'
    return item
  })
  total.value = tableDataList.total
}, 500)
onMounted(() => {
  getTableList()
})
watch(formList2.value, () => {
  getTableList()
})
const tableSelect = ref([])
const vendorNameSelectList = async (row: any) => {
  tableSelect.value.forEach((item: any, index: any) => {
    if (item === null) return
    row.vendorName.options.push({
      label: item.vendorName,
      value: `${item.vendorNo} ${item.vendorName}`, //中间空两个
      LeftLabel: `${item.vendorNo}`,
      RightLabel: `${item.vendorName}`
    })
  })
}
onMounted(async () => {
  tableSelect.value = await service.get<any, any>('/back/vendorQuery')
})
const selectListShow = (row: any, visible: boolean) => {
  if (visible === true) {
    console.log(11111)
    vendorNameSelectList(row)
  } else {
    console.log('remove')
    row.vendorName.options = []
  }
}
// 修改后点击确定按钮
const confirmChange = async (row: any) => {
  const copyData = formateData([row])
  const res = (await LoginWork.post(`/material/mtl/createSdMtlNew?choose=${3}`, {
    ...copyData[0],
    vendorName: copyData[0].vendorName.split(' ')[0],
    vendorNo: copyData[0].vendorName.split(' ')[1]
  })) as any
  if (res.code === 200) {
    JMessage.success('修改成功')
    getTableList()
  } else {
    JMessage.error(res.message)
  }
}

// 删除当前行
const removeOne = async () => {
  const copyData = formateData([currentTable1Row.value])
  const res = (await LoginWork.post(`/material/mtl/createSdMtlNew?choose=${4}`, {
    ...copyData[0],
    vendorName: copyData[0].vendorName.split(' ')[0],
    vendorNo: copyData[0].vendorName.split(' ')[1]
  })) as any
  if (res.code === 200) {
    JMessage.success('删除成功')
  } else {
    JMessage.error(res.message)
  }
  getTableList()
}
// 双击input置空
const inputDBclick = (item: any) => {
  if (item.inputDisabled) {
    item.inputValue = ''
  }
}
</script>

<style lang="less" scoped>
:deep(.el-input) {
  height: 25px;
  font-size: 14px;
}

:deep(.is-current) {
  background-color: #f5f7fa;

  .el-tree-node:focus > .el-tree-node__content {
    color: #f89027;
  }
}

:deep(.is-current > .el-tree-node__content:nth-child(1)) {
  color: #f89027;
}

:deep(.el-tree-node__content:hover) {
  color: #f89027;
}

:deep(.is-expanded > .el-tree-node__content > .el-tree-node__expand-icon) {
  color: red !important;
}

.MainCardDivMain {
  height: 90%;
  border: 1px solid #e6e6e6;
  margin-left: 10px;
  width: 335px;
}

.input {
  width: 101px;
}

:deep(.is-expanded > .el-tree-node__content > .el-tree-node__expand-icon) {
  color: red !important;
}
</style>
